package com.effectiv.crm.controller;

import javax.inject.Inject;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.effectiv.crm.domain.Potential;

import com.effectiv.crm.service.api.PickListService;
import com.effectiv.crm.service.api.PotentialService;

@Slf4j
@Controller
@RequestMapping("/potential")
public class PotentialController {

	@Inject
	private PotentialService service;
	
	@Inject
	private PickListService pickListService;

	@RequestMapping("/list")
	public String list(Model model) {
		model.addAttribute(service.findByDeleted(false));
		return "listpotentials";
	}

	@RequestMapping(value = { "/edit", "/new", "/details" })
	public String editOrNew(
			Model model,
			@RequestParam(value = "id", required = false) String id,
			@RequestParam(value = "view", required = false, defaultValue = "false") boolean view) {
		Potential potential = null;

		boolean edit = false;
		boolean newLead = false;

		if (StringUtils.isEmpty(id)) {
			potential = BeanUtils.instantiate(Potential.class);
			newLead = true;

		} else {
			potential = service.findOne(id);

			// handle missing potential
			if (potential == null) {
				// TODO - Return error and move back to list page
				potential = BeanUtils.instantiate(Potential.class);

			}

			if (!view) {
				edit = true;
			}
		}

		model.addAttribute(potential);
		model.addAttribute("salesStageList",
				pickListService.findByDomain("SalesStage"));
		model.addAttribute("potentialTypes",
				pickListService.findByDomain("PotentialType"));
		model.addAttribute("sourceList",
				pickListService.findByDomain("LeadSource"));
		
		
		model.addAttribute("view", view);
		model.addAttribute("edit", edit);
		model.addAttribute("newLead", newLead);
		model.addAttribute("id", id);

		return "newpotential";
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(@Valid Potential potential,
			BindingResult result, Model model) {
		
		log.info("Potential submitted for update - {}", potential);
		
		if (result.hasErrors()) {
			return "newlead";
		}
		else{
			service.update(potential);
			model.addAttribute("message",
					"Potential updated successfully.");
		}
		
		
		model.addAttribute(potential);
		model.addAttribute("salesStageList",
				pickListService.findByDomain("SalesStage"));
		model.addAttribute("potentialTypes",
				pickListService.findByDomain("PotentialType"));
		model.addAttribute("sourceList",
				pickListService.findByDomain("LeadSource"));
		model.addAttribute("view", false);
		model.addAttribute("edit", true);
		model.addAttribute("newLead", false);
		model.addAttribute("id", potential.getId());
		
		return "newpotential";
	}
	
	
	@RequestMapping("/copy")
	public String copy(Model model,@RequestParam(value="id") String id) {
		Potential potential = service.findOne(id);
		if(potential != null ){
			potential.setId(null);
			potential.setName("COPIED - " + potential.getName());
		}
		model.addAttribute(potential);
		model.addAttribute("salesStageList",
				pickListService.findByDomain("SalesStage"));
		model.addAttribute("potentialTypes",
				pickListService.findByDomain("PotentialType"));
		model.addAttribute("sourceList",
				pickListService.findByDomain("LeadSource"));
		model.addAttribute("view", false);
		model.addAttribute("edit", false);
		model.addAttribute("newLead", true);
		model.addAttribute("id", null);
		
		return "newpotential";
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String delete(@RequestParam("id") String id,
			final RedirectAttributes redirectAttributes){
		Potential potential = new Potential();
		potential.setId(id);
		
		service.delete(potential);
		
		redirectAttributes.addFlashAttribute("message", "Potential deleted successfully.");
		
		return "redirect:list";
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(@Valid Potential potential,
			BindingResult result, Model model) {
		
		
		if (result.hasErrors()) {
			log.info("Result - {}",result);
			return "newpotential";
		}
		else{
			
			log.info("Saving lead");
			potential.setId(null);
			service.save(potential);
			model.addAttribute("message",
					"Potential saved successfully.");
		}

		
		model.addAttribute(potential);
		model.addAttribute("salesStageList",
				pickListService.findByDomain("SalesStage"));
		model.addAttribute("potentialTypes",
				pickListService.findByDomain("PotentialType"));
		model.addAttribute("sourceList",
				pickListService.findByDomain("LeadSource"));
		model.addAttribute("view", true);
		model.addAttribute("edit", false);
		model.addAttribute("newLead", false);
		model.addAttribute("id", potential.getId());
		
		
		return "newpotential";
	}
}
